package com.uenpay.im.ui.old.activity.company;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;

import java.util.ArrayList;


/**
 * 只有部门成员列表
 *
 * @author SL
 * @created 2016-3-19 13:15:39
 * @copyRight http://www.qiyunxin.com
 */
public class OnlyEmployeeListActivity extends BaseActivity {

	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private EmployeeAdapter mEmployeeAdapter = null;
	private DepartmentEntity departmentEntity = null;
	private ListView employee_listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_layout);
		IMApplication.getInstance().addActivity(this);
		departmentEntity = (DepartmentEntity) getIntent().getSerializableExtra(
				"departmentEntity");
		initView();
		initListener();
		backListener();
		getCustByDepart();
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.approval_of_people);
		employee_listview = (ListView) findViewById(R.id.listview);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.sure);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		loading = findViewById(R.id.loading);
	}

	@Override
	protected void initListener() {
		employee_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				EmployeeEntity mEmployeeEntity = (EmployeeEntity) arg0
						.getAdapter().getItem(arg2);
				if (mEmployeeEntity != null) {
					mEmployeeAdapter.setSelected(mEmployeeEntity.custid);

				}
			}

		});
		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						if (mEmployeeAdapter == null)
							return;
						EmployeeEntity mEmployeeEntity = mEmployeeAdapter
								.getSelectedUser();
						if (mEmployeeEntity != null) {
							Intent intent = new Intent();
							intent.putExtra("mEmployeeEntity", mEmployeeEntity);
							setResult(RESULT_OK, intent);
							finish();
						}
					}
				});
	}

	private void getCustByDepart() {
		mCompanyHandle.getCustbyDepart(departmentEntity.departid,
				departmentEntity.entid, new CompanyHandle.IGetCustByDepartListener() {

					@Override
					public void onGetCustByDepartResult(int status, String msg,
							ArrayList<EmployeeEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (arrayList != null && arrayList.size() > 0) {
							for (int i = 0, size = arrayList.size(); i < size; i++) {
								arrayList.get(i).is_selected = 0;
							}
							mEmployeeAdapter = new EmployeeAdapter(
									OnlyEmployeeListActivity.this, arrayList,
									false);
							employee_listview.setAdapter(mEmployeeAdapter);
						}
					}
				});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

}
